// Fondo.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "AboutFondo.h"
#include "Fondo.h"
#include "MainFrm.h"
#include "ImageThread.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFondoApp

BEGIN_MESSAGE_MAP(CFondoApp, CWinApp)
	ON_COMMAND(ID_POPUP_ABOUTFONDO, OnAppAbout)
	ON_COMMAND(ID_POPUP_SETTINGS, OnSettings)
END_MESSAGE_MAP()


// CFondoApp construction

CFondoApp::CFondoApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CFondoApp object

CFondoApp theApp;


// CFondoApp initialization

BOOL CFondoApp::InitInstance()
{
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();

	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->Create(NULL, "Fondo"))
		return FALSE;
	m_pMainWnd = pMainFrame;
	
	// Now set up the app to get the directory,
	// rotate time, etc.
	Setup();

	return TRUE;
}

void CFondoApp::OnAppAbout()
{
	CAboutFondo aboutDlg;
	aboutDlg.DoModal();
}

void CFondoApp::OnSettings()
{
	CFondoDlg dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		rotateTime = dlg.getMinutes();

		CString directory(dlg.getDirectory());
		imageDirectory = directory.GetBuffer();
		directory.ReleaseBuffer();

		// And tell the thread we reloaded...
		::SetEvent(interruptEvent);

		SaveNewValues();
	}
}

void CFondoApp::SaveNewValues(void)
{
	HKEY hKey = NULL;
	char szKey[200] = {0};

    strcpy(szKey, "SOFTWARE\\StrangeBreeds\\Fondo");

	if (RegCreateKey(HKEY_CURRENT_USER, szKey, &hKey) == ERROR_SUCCESS)
	{
	    RegSetValueEx(hKey,
					  "Rotate Time",
					  NULL,
					  REG_DWORD,
					  (CONST BYTE *)&rotateTime,
					  sizeof(DWORD));

		RegSetValueEx(hKey,
					  "Image Directory",
					  NULL,
					  REG_SZ,
					  (CONST BYTE *)imageDirectory.c_str(),
					  imageDirectory.length() + 1);

		RegCloseKey(hKey);
	}
}

DWORD CFondoApp::GetRotateTime(void)
{
    HKEY hKey = NULL;
	char szKey[200] = {0};

	DWORD dwType = REG_DWORD;
	DWORD value = 0;
	size_t cbVal = sizeof( value );

    strcpy(szKey, "SOFTWARE\\StrangeBreeds\\Fondo");

	if (RegCreateKey(HKEY_CURRENT_USER, szKey, &hKey) == ERROR_SUCCESS)
	{

	    RegQueryValueEx(hKey,
						"Rotate Time",
						NULL,
						&dwType,
						(LPBYTE)&value,
						(LPDWORD)&cbVal );

		RegCloseKey(hKey);
	}

	return value;
}

CString CFondoApp::GetImageDirectory(void)
{
    HKEY hKey = NULL;
	char szKey[200] = {0};

	DWORD dwType = REG_SZ;
	char szDirectory[1024] = {0};
	size_t cbVal = sizeof(szDirectory);

    strcpy(szKey, "SOFTWARE\\StrangeBreeds\\Fondo");

	if (RegCreateKey(HKEY_CURRENT_USER, szKey, &hKey) == ERROR_SUCCESS)
	{

	    if(RegQueryValueEx(hKey, 
						   "Image Directory", 
						   NULL, 
						   &dwType, 
						   (LPBYTE)&szDirectory, 
						   (LPDWORD)&cbVal ) != ERROR_SUCCESS )
		{
			sprintf(szDirectory, ".");
		}

		RegCloseKey(hKey);
	}
	
	return szDirectory;
}

void CFondoApp::Setup()
{
	rotateTime = GetRotateTime();
	if (rotateTime == 0)
		rotateTime = 10; // 10 minutes as the default
	
	imageDirectory = GetImageDirectory();

	interruptEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);

	// Now start our thread
	AfxBeginThread(ImageThread, (void*)0);
}